package com.gaga.metadata.convert;


import com.gaga.metadata.common.assertion.Asserts;
import com.gaga.metadata.common.enums.ColumnType;
import com.gaga.metadata.entity.database.DataBaseColumn;

/**
 * 数据类型转换 DorisTypeConvert
 **/
public class MysqlTypeConvert extends AbstractDataTypeConvert {

    @Override
    public ColumnType convert(DataBaseColumn column) {
        ColumnType columnType = ColumnType.STRING;
        if (Asserts.isNull(column)) {
            return columnType;
        }
        String t = column.getDbType().toLowerCase();
        if (t.contains("tinyint")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("smallint") || t.contains("tinyint unsigned")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("bigint unsigned") || t.contains("numeric") || t.contains("decimal")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("bigint") || t.contains("int unsigned")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("float")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("double")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("boolean") || t.contains("tinyint(1)")) {
            columnType = ColumnType.NUMBER;
        } else if (t.contains("datetime") || t.contains("timestamp")) {
            columnType = ColumnType.DATETIME;
        } else if (t.contains("date")) {
            columnType = ColumnType.DATE;
        } else if (t.contains("char") || t.contains("text")) {
            columnType = ColumnType.STRING;
        } else if (t.contains("binary") || t.contains("blob")) {
            columnType = ColumnType.TEXT;
        } else if (t.contains("int") || t.contains("mediumint") || t.contains("smallint unsigned")) {
            columnType = ColumnType.NUMBER;
        }
        return columnType;
    }

    @Override
    public String convertToDB(DataBaseColumn column) {
        ColumnType columnType = column.getColumnType();
        switch (columnType) {
            case DATETIME:
                return "TIMESTAMP";
            case DATE:
                return "DATE";
            case NUMBER:
                if (column.getScale().intValue() > 0) {
                    return "DECIMAL";
                } else {
//                    if (column.getPrecision() != null) {
//                        if (column.getPrecision() <= 2) {
//                            return "TINYINT";
//                        } else if (column.getPrecision() <= 4) {
//                            return "SMALLINT";
//                        } else if (column.getPrecision() <= 8) {
//                            return "INT";
//                        } else if (column.getPrecision() <= 16) {
//                            return "BIGINT";
//                        } else {
//                            return "LARGEINT";
//                        }
//                    }
                    return "INT";
                }
            case STRING:
                if (column.getPrecision() != null) {
                    if (column.getPrecision() <= 1) {
                        return "CHAR";
                    }
                }
                return "VARCHAR";
            default:
                return "VARCHAR";
        }
    }

}
